﻿using Modelado.Modulos.ClasesModelado;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AccesoDatos.Modulos.ClasesAccesoDatos
{
    public class AD_Modulo
    {
        public void CRE_Modulo(SqlCommand comando, MOD_Modulo obj)
        {
            try
            {
                string sp = "SP_CRE_Modulo";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@MODU_NOMBRE", SqlDbType.VarChar));

                comando.Parameters[0].Value = obj.MODU_nombre;

                comando.ExecuteNonQuery();
            }
            catch (Exception exc)
            {
                
                throw exc;
            }
        }

        public void DEL_Modulo(SqlCommand comando, int codigo)
        {
            try
            {
                string sp = "SP_DEL_Modulo";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@MODU_CODMODULO", SqlDbType.Int));

                comando.Parameters[0].Value = codigo;

                comando.ExecuteNonQuery();

            }
            catch (Exception exc)
            {
                
                throw exc;
            }
        }


        public void UPD_Modulo(SqlCommand comando, MOD_Modulo obj)
        {
            try
            {
                string sp = "SP_UPD_Modulo";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@MODU_CODMODULO", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@MODU_NOMBRE", SqlDbType.VarChar));

                comando.Parameters[0].Value = obj.MODU_codmodulo;
                comando.Parameters[1].Value = obj.MODU_nombre;

                comando.ExecuteNonQuery();
            }
            catch (Exception exc)
            {
                
                throw exc;
            }
        }


        public List<MOD_Modulo> SEL_Grid_Modulo(SqlCommand comando)
        {
            try
            {
                string sp = "SP_SEL_Grid_Modulo";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                List<MOD_Modulo> lista = new List<MOD_Modulo>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MOD_Modulo obj = new MOD_Modulo();
                    obj.MODU_codmodulo = Convert.ToInt32(dt.Rows[i]["MODU_CODMODULO"]);
                    obj.MODU_nombre = dt.Rows[i]["MODU_NOMBRE"].ToString();

                    lista.Add(obj);
                }

                return lista;
            }
            catch (Exception exc )
            {
                
                throw exc;
            }
        }

        public MOD_Modulo SEL_ModuloByID(SqlCommand comando, int MODU_CODMODULO)
        {
            try
            {
                string sp = "SP_SEL_ModuloByID";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@MODU_CODMODULO", SqlDbType.Int));

                comando.Parameters[0].Value = MODU_CODMODULO;

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                MOD_Modulo obj = new MOD_Modulo();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    obj.MODU_codmodulo = Convert.ToInt32(dt.Rows[i]["MODU_CODMODULO"]);
                    obj.MODU_nombre = dt.Rows[i]["MODU_NOMBRE"].ToString();
                }

                return obj;
            }
            catch (Exception exc)
            {
                
                throw exc;
            }
        }

        public int  SEL_ModuloByNombre(SqlCommand comando, string MODU_NOMBRE)
        {
            try
            {
                string sp = "SP_SEL_ModuloByNOmbre";
                int cant = 0;
                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@MODU_NOMBRE", SqlDbType.VarChar));

                comando.Parameters[0].Value = MODU_NOMBRE;

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                MOD_Modulo obj = new MOD_Modulo();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cant = Convert.ToInt32(dt.Rows[i]["MODULO_CANT"]);
                }

                return cant;
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }
    }

    
}
